﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;

namespace cczEdit
{
    public partial class UCShopData : UserControl
    {
        private PListRoot root;
        List<Sundry> sundries;
        List<Equipment> weapons;
        List<Equipment> armors;
        List<Equipment> assists;
        public UCShopData()
        {
            InitializeComponent();
            sundries = new List<Sundry>();
            weapons = new List<Equipment>();
            armors = new List<Equipment>();
            assists = new List<Equipment>();
            root = PListRoot.Load(Form1.cczDirector + "/ShopData.plist");
            LoadItemData(Form1.cczDirector + "/ItemData.plist");
            LoadShopData();

        }
        void LoadShopData()
        {
            listView1.Items.Clear();
            PListDict rootDict = (PListDict)root.Root;
            Dictionary<string,IPListElement> dictOrdered= rootDict.OrderBy(k => ((PListInteger)((PListDict)k.Value)["Num"]).Value).ToDictionary(k => k.Key, v => v.Value);;
            foreach (KeyValuePair<String, IPListElement> kv in dictOrdered)
            {
                listView1.Items.Add(kv.Key);
            }
            dataGridView1.Rows.Clear();
            dataGridView2.Rows.Clear();
            initItemList(weapons, "WeaponsEnable");
            initItemList(armors, "ArmorsEnable");
            initItemList(assists, "AssistsEnable");
            initItemList(sundries, "SundriesEnable");
            if (dictOrdered.Count > 0)
            {
                listView1.Items[dictOrdered.Count-1].Selected = true;
            }

        }
        public void SaveAllData()
        {
            PListDict rootDict = (PListDict)root.Root;
            PListDict shopItem = (PListDict)rootDict[listView1.SelectedItems[0].Text];
            PListArray weaponsEnable = (PListArray)shopItem["WeaponsEnable"];
            PListArray armorsEnable = (PListArray)shopItem["ArmorsEnable"];
            PListArray assistEnable = (PListArray)shopItem["AssistsEnable"];
            PListArray sundriesEnable = (PListArray)shopItem["SundriesEnable"];
            weaponsEnable.Clear();
            armorsEnable.Clear();
            assistEnable.Clear();
            sundriesEnable.Clear();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if ((bool)row.Cells["ColumnCheck"].Value)
                {
                    PListArray array = (PListArray)shopItem[row.Cells["ColumnType"].Value.ToString()];
                    array.Add(new PListInteger(long.Parse(row.Cells["ColumnNum"].Value.ToString())));
                }
            }
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                if ((bool)row.Cells["ColumnCheck2"].Value)
                {
                    PListArray array = (PListArray)shopItem[row.Cells["ColumnType2"].Value.ToString()];
                    array.Add(new PListInteger(long.Parse(row.Cells["ColumnNum2"].Value.ToString())));
                }
            }
            root.Save(Form1.cczDirector + "/Shopdata.plist");
        }
        private void LoadItemData(String fileName_)
        {

            PListRoot newRoot = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)newRoot.Root;
            PListDict weaponsDic = (PListDict)rootDic["Weapon"];
            PListDict armorsDic = (PListDict)rootDic["Armor"];
            PListDict assistsDic = (PListDict)rootDic["Assist"];
            PListDict SundrisDic = (PListDict)rootDic["Sundry"];
            foreach (KeyValuePair<String, IPListElement> a in weaponsDic)
            {
                PListDict weaponDic = (PListDict)a.Value;
                PListString name = (PListString)weaponDic["Name"];

                PListInteger num = (PListInteger)weaponDic["Num"];
                PListInteger numIcon = (PListInteger)weaponDic["NumIcon"];

                PListString dspt = (PListString)weaponDic["Dspt"];
                PListInteger grow = (PListInteger)weaponDic["Grow"];
                PListInteger numEffect = (PListInteger)weaponDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)weaponDic["ValueEffect"];
                PListInteger price = (PListInteger)weaponDic["Price"];
                PListInteger type = (PListInteger)weaponDic["Type"];
                PListInteger valueAbility = (PListInteger)weaponDic["ValueAbility"];

                Equipment weapon = new Equipment();
                weapon.name = name.Value;
                weapon.num = num.Value;
                weapon.dspt = dspt.Value;
                weapon.numEffect = numEffect.Value;
                weapon.valueEffect = valueEffect.Value;
                weapon.numIcon = Convert.ToInt64(numIcon.Value);
                weapon.price = price.Value;

                weapon.grow = grow.Value;
                weapon.teamEnable = 0;
                weapon.type = type.Value;
                weapon.valueAbility = valueAbility.Value;
                weapons.Add(weapon);

            }
            foreach (KeyValuePair<String, IPListElement> a in armorsDic)
            {
                PListDict armorDic = (PListDict)a.Value;
                PListString name = (PListString)armorDic["Name"];

                PListInteger num = (PListInteger)armorDic["Num"];
                PListInteger numIcon = (PListInteger)armorDic["NumIcon"];

                PListString dspt = (PListString)armorDic["Dspt"];
                PListInteger grow = (PListInteger)armorDic["Grow"];
                PListInteger numEffect = (PListInteger)armorDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)armorDic["ValueEffect"];
                PListInteger price = (PListInteger)armorDic["Price"];
                PListInteger type = (PListInteger)armorDic["Type"];
                PListInteger valueAbility = (PListInteger)armorDic["ValueAbility"];

                Equipment armor = new Equipment();
                armor.name = name.Value;
                armor.num = num.Value;
                armor.dspt = dspt.Value;
                armor.numEffect = numEffect.Value;
                armor.valueEffect = valueEffect.Value;
                armor.numIcon = Convert.ToInt64(numIcon.Value);
                armor.price = price.Value;

                armor.grow = grow.Value;
                armor.teamEnable = 0;
                armor.type = type.Value;
                armor.valueAbility = valueAbility.Value;
                armors.Add(armor);
            }
            foreach (KeyValuePair<String, IPListElement> a in assistsDic)
            {
                PListDict assistDic = (PListDict)a.Value;
                PListString name = (PListString)assistDic["Name"];

                PListInteger num = (PListInteger)assistDic["Num"];
                PListInteger numIcon = (PListInteger)assistDic["NumIcon"];
                PListInteger teamEnable = (PListInteger)assistDic["TeamEnable"];
                PListString dspt = (PListString)assistDic["Dspt"];
                PListInteger numEffect = (PListInteger)assistDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)assistDic["ValueEffect"];
                PListInteger price = (PListInteger)assistDic["Price"];

                PListInteger valueAbility = (PListInteger)assistDic["ValueAbility"];

                Equipment assist = new Equipment();
                assist.name = name.Value;
                assist.num = num.Value;
                assist.dspt = dspt.Value;
                assist.numEffect = numEffect.Value;
                assist.valueEffect = valueEffect.Value;
                assist.numIcon = Convert.ToInt64(numIcon.Value);
                assist.price = price.Value;

                assist.grow = 0;


                assist.valueAbility = valueAbility.Value;
                assists.Add(assist);
            }
            foreach (KeyValuePair<String, IPListElement> a in SundrisDic)
            {
                PListDict sundryDic = (PListDict)a.Value;
                PListString name = (PListString)sundryDic["Name"];

                PListInteger num = (PListInteger)sundryDic["Num"];
                PListInteger numIcon = (PListInteger)sundryDic["NumIcon"];
                PListString dspt = (PListString)sundryDic["Dspt"];
                PListInteger numEffect = (PListInteger)sundryDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)sundryDic["ValueEffect"];
                PListInteger price = (PListInteger)sundryDic["Price"];



                Equipment sundry = new Equipment();
                sundry.name = name.Value;
                sundry.num = num.Value;
                sundry.dspt = dspt.Value;
                sundry.numEffect = numEffect.Value;
                sundry.valueEffect = valueEffect.Value;
                sundry.numIcon = numIcon.Value;
                sundry.price = price.Value;

                sundry.grow = 0;
                sundry.teamEnable = 0;
                sundry.type = 0;
                sundry.valueAbility = 0;
                sundries.Add(sundry);

            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }

            refreshItemsChecked(true);
            refreshItemsChecked(false);
            PListDict rootDict = (PListDict)root.Root;
            PListDict dict = (PListDict)rootDict[listView1.SelectedItems[0].Text];
            textBox1.Text = ((PListInteger)dict["Num"]).Value.ToString();
        }


        private void initItemList(List<Sundry> list,string itemString)
        {
            
            foreach (Sundry sundry in list)
            {
                DataGridViewRow row = dataGridView2.Rows[dataGridView2.Rows.Add()];

                row.Cells["ColumnCheck2"].Value = false;
                row.Cells["ColumnName2"].Value = sundry.name;
                row.Cells["ColumnNum2"].Value = sundry.num;
                row.Cells["ColumnType2"].Value = itemString;
            }

            
        }
        private void initItemList(List<Equipment> list, string itemString)
        {
            
            foreach (Equipment equip in list)
            {   
                DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Add()];
                row.Cells["ColumnCheck"].Value = false;
                row.Cells["ColumnName"].Value = equip.name;
                row.Cells["ColumnNum"].Value = equip.num;
                row.Cells["ColumnType"].Value = itemString;
            }

        }
        private void refreshItemsChecked(bool isSundries)
        {
            PListDict rootDict = (PListDict)root.Root;
            PListDict dict = (PListDict)rootDict[listView1.SelectedItems[0].Text];
            
            DataGridView dataGridView = dataGridView1;
            string num = "";
            if (isSundries)
            {
                dataGridView = dataGridView2;
                num = "2";
            }
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                row.Cells["ColumnCheck" + num].Value = false;
                PListArray array = (PListArray)dict[row.Cells["ColumnType" + num].Value.ToString()];
                foreach (PListInteger item in array)
                {
                    if (item.Value.ToString() ==row.Cells["ColumnNum" + num].Value.ToString())
                        row.Cells["ColumnCheck" + num].Value = true;
                }
            }
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            PListDict rootDict = (PListDict)root.Root;
            PListDict dict = (PListDict)rootDict[listView1.SelectedItems[0].Text];
            if (rootDict.Keys.Contains("Shop" + textBox1.Text))
            {
                MessageBox.Show("重复的R剧本编号");
                return;
            }
            PListDict newShopItem = new PListDict();
            PListArray weaponsEnable = new PListArray();
            PListArray armorsEnable = new PListArray();
            PListArray assistEnable = new PListArray();
            PListArray sundriesEnable = new PListArray();
            PListInteger num = new PListInteger(long.Parse( textBox1.Text));
            newShopItem.Add("Num",num);
            newShopItem.Add("WeaponsEnable",weaponsEnable);
            newShopItem.Add("ArmorsEnable",armorsEnable);
            newShopItem.Add("AssistsEnable",assistEnable);
            newShopItem.Add("SundriesEnable",sundriesEnable);
            rootDict.Add("Shop" + textBox1.Text, newShopItem);
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if ((bool)row.Cells["ColumnCheck"].Value)
                {
                    PListArray array = (PListArray)newShopItem[row.Cells["ColumnType"].Value.ToString()];
                    array.Add(new PListInteger(long.Parse(row.Cells["ColumnNum"].Value.ToString())));
                }
            }
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                if ((bool)row.Cells["ColumnCheck2"].Value)
                {
                    PListArray array = (PListArray)newShopItem[row.Cells["ColumnType2"].Value.ToString()];
                    array.Add(new PListInteger(long.Parse(row.Cells["ColumnNum2"].Value.ToString())));
                }
            }
            LoadShopData();   
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveAllData();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            PListDict rootDict = (PListDict)root.Root;
            PListDict dict = (PListDict)rootDict[listView1.SelectedItems[0].Text];
            rootDict.Remove(listView1.SelectedItems[0].Text);
            ListViewItem item = listView1.SelectedItems[0];
            listView1.Items.Remove(item);
        }
    }
}
